package cn.itsource.xhy.org.controller;

import cn.itsource.xhy.org.domain.SystemDetall;
import cn.itsource.xhy.org.query.SystemDetallQuery;
import cn.itsource.xhy.org.service.ISystemDetallService;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdetall")
public class SystemDetallController {

    @Autowired
    private ISystemDetallService systemDetallService;

    @PutMapping // put请求添加和修改，在参数中加上@RequestBody注解接收json格式数据，转换成对象
    public AjaxResult saveOrUpdate(@RequestBody SystemDetall systemDetall){
        try {
            if(systemDetall.getId()==null){
                systemDetallService.save(systemDetall);
            } else {
                systemDetallService.update(systemDetall);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            systemDetallService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public SystemDetall findById(@PathVariable("id") Long id){
        try {
            return systemDetallService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping
    public List<SystemDetall> findAll(){
        try {
            return systemDetallService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    public QueryResult<SystemDetall> queryPage(@RequestBody SystemDetallQuery systemDetallQuery){
        System.out.println(systemDetallQuery);
        return systemDetallService.queryPage(systemDetallQuery);
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            systemDetallService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

}
